package com.hardydou.system.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemConfig;
import com.hardydou.system.service.ISystemConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：系统参数
 * @date ：Created in 2020/6/13
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/system/config")
@Api(tags = "系统参数")
@LogMetaInfo(module = "系统参数")
public class SysConfigController extends BaseController {
    public final static String model = "sys";
    public final static String subModel = "config";
    @Autowired
    ISystemConfigService configService;


    @ApiIgnore
    @GetMapping("")
    public String index() {
        return page("list");
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return page("add");
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemConfig config, Model model) {
        configService.add(config);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return page("add");
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long configId, Model model) {
        SystemConfig config = configService.findById(configId);
        model.addAttribute("config", config);
        return page("edit");
    }

    @ApiIgnore
    @PutMapping("/edit")
    public String edit(SystemConfig config, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        configService.update(config);
        model.addAttribute("config", config);
        return page("edit");
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long configId, Model model) {
        SystemConfig config = configService.findById(configId);
        model.addAttribute("config", config);
        return page("info");
    }


    /* JSON API */
    @ApiOperation(value = "系统参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        List<SystemConfig> configs = configService.list(params);
        PageInfo pageInfo = new PageInfo(configs);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加系统参数")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemConfig config) {
        configService.add(config);
        return ResultHelper.ok(config);
    }


    @ApiOperation(value = "删除系统参数")
    @ApiImplicitParam(name = "configId", value = "参数ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(long configId) {
        configService.deleteById(configId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("configId", configId);
        return result;
    }

    @ApiOperation(value = "更新系统参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId", value = "参数ID", required = true, paramType = "query")
    })
    @ApiParam(name = "configId", value = "参数ID", required = true)
    @PutMapping("/update")
    @ResponseBody
    public Result update(SystemConfig config) {
        configService.update(config);
        return ResultHelper.ok(config);
    }

    @Override
    public String getModel() {
        return model;
    }

    @Override
    public String getSubModel() {
        return subModel;
    }


}
